import urllib.request
import urllib.parse
def create_reuqest(page):
    '''
    获取request请求对象
    :param page:
    :return:
    '''
    base_url = 'https://movie.douban.com/j/chart/top_list?type=20&interval_id=100%3A90&action=&'
    headers = {
        'User‐Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome / 76.0.3809.100Safari / 537.36'
    }
    data = {
    # 1 2 3 4
    # 0 20 40 60
    'start': (page - 1) * 20,
    'limit': 20
    }
    data = urllib.parse.urlencode(data).encode('utf-8')
    req = urllib.request.Request(url=base_url,headers=headers,data=data)
    return req
def get_content(request):
    '''
    获取request请求内容
    :param request:
    :return:
    '''
    response = urllib.request.urlopen(request)
    return response.read().decode('utf-8')
def download_content(page,content):
    '''下载页面内容'''
    with open('douban_'+str(page)+'.json','w',encoding='utf-8') as fp:
        fp.write(content)
if __name__ == '__main__':
    start_page = int(input('请输入起始页码'))
    end_page = int(input('请输入结束页码'))
    for page in range(start_page,end_page+1):
        #获取request请求对象
        request = get_content(page)
        content = create_reuqest(request)
        download_content(page,content)
